Skip to content

SmoothScrollBar

Base classes

Name Children Inherits
ScrollBar
prettyqt.widgets.scrollbar
Vertical or horizontal scroll bar.

â‹” Inheritance diagram

graph TD
  1218384466["custom_widgets.SmoothScrollBar"]
  524491678["widgets.ScrollBar"]
  839910109["widgets.ScrollBarMixin"]
  3920585708["widgets.AbstractSliderMixin"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  3837360857["QtWidgets.QScrollBar"]
  229763307["QtWidgets.QAbstractSlider"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  524491678 --> 1218384466
  839910109 --> 524491678
  3920585708 --> 839910109
  829357528 --> 3920585708
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2751680898 --> 3920585708
  2751680898 --> 839910109
  3837360857 --> 524491678
  229763307 --> 3837360857
  3620195106 --> 229763307
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: ScrollBar

Source code in prettyqt/custom_widgets/scrollbars/smoothscrollbar.py
class SmoothScrollBar(widgets.ScrollBar):
    scroll_ended = core.Signal()

    def __init__(
        self,
        orientation: constants.Orientation | constants.OrientationStr = "horizontal",
        parent: widgets.QAbstractScrollArea | None = None,
        animation_duration: int = 500,
        easing: core.easingcurve.TypeStr | core.QEasingCurve.Type = "out_cubic",
        trigger: bool = False,
    ):
        super().__init__(orientation, parent)
        self._value = self.value()
        self.widget = parent
        self._animation = core.PropertyAnimation()
        self._animation.apply_to(self.value)
        self._animation.set_easing(easing)
        self._animation.setDuration(animation_duration)
        self._animation.finished.connect(self.scroll_ended)
        if self.widget is not None:
            self.widget.viewport().installEventFilter(self)
            if trigger:
                self.widget.horizontalScrollBar().valueChanged.connect(
                    gui.Cursor.fake_mouse_move
                )

    def mouseMoveEvent(self, e):
        self._animation.stop()
        self._value = self.value()
        super().mouseMoveEvent(e)

    def mousePressEvent(self, e):
        self._animation.stop()
        self._value = self.value()
        super().mousePressEvent(e)

    def mouseReleaseEvent(self, e):
        self._animation.stop()
        self._value = self.value()
        super().mouseReleaseEvent(e)

    def setValue(self, value: int):
        if value == self.value():
            return
        self._animation.stop()
        self.scroll_ended.emit()
        self._animation.set_range(self.value(), value)
        self._animation.start()

    def scroll_by_value(self, value: int):
        """Scroll by given distance."""
        self._value += value
        self._value = min(max(self.minimum(), self._value), self.maximum())
        self.setValue(self._value)

    def scroll_to(self, value: int):
        """Scroll to given position."""
        self._value = value
        self._value = min(max(self.minimum(), self._value), self.maximum())
        self.setValue(self._value)

    def reset_value(self, value):
        self._value = value

    def eventFilter(self, source, event) -> bool:
        if (
            self.widget is not None
            and event.type() == core.Event.Type.Wheel
            and source == self.widget.viewport()
        ):
            v_bar = self.widget.verticalScrollBar()
            if isinstance(v_bar, SmoothScrollBar):
                v_bar.scroll_by_value(-event.angleDelta().y())
            else:
                v_bar.setValue(v_bar.value() - event.angleDelta().y())
            return True
        return False

    def set_animation_duration(self, duration: int):
        self._animation.setDuration(duration)

    def get_animation_duration(self) -> int:
        return self._animation.duration()

    def set_animation_easing(
        self,
        easing: core.easingcurve.TypeStr | core.QEasingCurve.Type = "out_cubic",
    ):
        self._animation.set_easing(easing)

    def get_animation_easing(
        self,
    ) -> core.easingcurve.TypeStr | Callable[[float], float]:
        return self._animation.get_easing()

    def animationEasing(self) -> core.QEasingCurve.Type:
        return self._animation.easingCurve().type()

    animation_duration = core.Property(
        int,
        get_animation_duration,
        set_animation_duration,
        doc="Duration for the animation",
    )
    animation_easing = core.Property(
        core.EasingCurve.Type,
        get_animation_easing,
        set_animation_easing,
        doc="Animation easing type",
    )

scroll_by_value(value: int)

Scroll by given distance.

Source code in prettyqt/custom_widgets/scrollbars/smoothscrollbar.py
def scroll_by_value(self, value: int):
    """Scroll by given distance."""
    self._value += value
    self._value = min(max(self.minimum(), self._value), self.maximum())
    self.setValue(self._value)

scroll_to(value: int)

Scroll to given position.

Source code in prettyqt/custom_widgets/scrollbars/smoothscrollbar.py
def scroll_to(self, value: int):
    """Scroll to given position."""
    self._value = value
    self._value = min(max(self.minimum(), self._value), self.maximum())
    self.setValue(self._value)

:fa-brands-github: Show source on GitHub